import 'package:flutter/material.dart';

class AppRoveMultipleItemView extends StatefulWidget {
  final List<String> items;
  final bool isSingleSelection;
  final Function(String, List<String>) onItemSelected;

  AppRoveMultipleItemView({required this.items, required this.isSingleSelection, required this.onItemSelected});

  @override
  _AppRoveMultipleItemViewState createState() => _AppRoveMultipleItemViewState();
}

class _AppRoveMultipleItemViewState extends State<AppRoveMultipleItemView> {
  String? _selectedItem;
  List<String> _selectedItems = [];

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      padding: EdgeInsets.all(0.0), // 设置所有边的padding为16.0
      physics: NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      itemCount: widget.items.length,
      itemBuilder: (context, index) {
        return ListTile(
          title: Text(widget.items[index]),
          trailing: Checkbox(
            value: widget.isSingleSelection ? _selectedItem == widget.items[index] : _selectedItems.contains(widget.items[index]),
            onChanged: (value) {
              setState(() {
                if (widget.isSingleSelection) {
                  _selectedItem = widget.items[index];
                  widget.onItemSelected(_selectedItem!, _selectedItems);
                } else {
                  if (value!) {
                    _selectedItems.add(widget.items[index]);
                  } else {
                    _selectedItems.remove(widget.items[index]);
                  }
                  widget.onItemSelected(widget.items[index], _selectedItems);
                }
              });
            },
          ),
          onTap: () {
            setState(() {
              if (widget.isSingleSelection) {
                _selectedItem = widget.items[index];
                widget.onItemSelected(_selectedItem!, _selectedItems);
              } else {
                if (_selectedItems.contains(widget.items[index])) {
                  _selectedItems.remove(widget.items[index]);
                } else {
                  _selectedItems.add(widget.items[index]);
                }
                widget.onItemSelected(widget.items[index], _selectedItems);
              }
            });
          },
        );
      },
    );
  }
}
